const sys = require(`./system.js`)
const title = '助学查盲盒开盲盒'
const $ = sys.$(title)

sys.run($, 'task:all', (resList, cookie, index, username, password, item) => {
  return new Promise(async resolve => {
    const ub = item['可用盲盒']
    console.log('可用盲盒检查', username, ub + '个盲盒');
    if(ub != 0) {
        const cookie = await $.publicJS.checkCookie($, $.localStorage.get(`${username}-cookie`, 'localStorage'), username, password)
        for (let i = 0; i < ub; i++) {
            const resp = await $.publicJS.useBox($, cookie)
            console.log('开启盲盒', username, resp.name);
        }
        item['可用盲盒'] = 0
        console.log('结束')
    }
    resList.push(item)
    resolve(resList)

  })
}, (resList) => {
  console.log('任务结束 resList:', resList)
  if (resList && resList.length > 0) {
    const resListStr = JSON.stringify(resList, null, 2)
    $.notify.sendNotify(title, resListStr)
  }
})
